🍭 Swear JS
@swear-js/core
Core package of SwearJS state manager
Frameworks:
Installation
$ npm install @swear-js/core
or in case you are using Yarn:
$ yarn add @swear-js/core
Usage
First you have to initialize your store.
import { createStore } from "@swear-js/core";
const store = createStore();
Then you have to create
import { createStore } from "@swear-js/core";
const store = createStore();
store.subscribe({
swearId: 'count',
defaultState: 0,
onUpdate: (newValue) => {
}
});
const decreaseHandler = () => {
store.setSwearValue('count', 'decrease', store.getSwearValue('count') - 1);
};
const increaseHandler = () => {
store.setSwearValue('count', 'increase', store.getSwearValue('count') + 1);
};
Logging
You can pass your custom logger to the store, or use @swear-js/logger.
Swear-js logger usage:
import { createStore } from "@swear-js/core";
import { swearLogger } from "@swear-js/logger";
const store = createStore({ onPatch: swearLogger });
In order to implement your own logger solution, you just have to keep in mind an API of onPatch
argument of the store.
onPatch
is any callback that get SwearPatch
(you can find it in package) type as an argument.
Simply SwearPatch is:
{
swearId,
actionType,
prev,
payload,
next
}
Example very-simple implementation of logger:
import { SwearPatch, createStore } from "@swear-js/core";
const logger = ({ swearId, actionType, prev, payload, next }: SwearPatch) => {
console.log(`Swear: ${swearId}, Action: ${actionType}, Payload: ${payload}`);
console.log(`Previous state: ${prev}`, `Current state: ${next}`);
};
const store = createStore({ onPatch: logger });